﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Trade.DAL;
using Trade.Model;
using Trade.Controls;

namespace Trade
{
    public partial class FrmVendor : RayForm
    {
        private Vendor vendor = new Vendor();
        //private VendorInfo vendorInfo;
        private Utility.CRUD dataState = Utility.CRUD.None;
        private Common common = new Common();
        private DataTable dtVendor = null;

        public FrmVendor()
        {
            InitializeComponent();
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);

            btnOperatorGroup.btnNew.Click += new EventHandler(btnNew_Click);
            btnOperatorGroup.btnModify.Click += new EventHandler(btnModify_Click);
            btnOperatorGroup.btnDelete.Click += new EventHandler(btnDelete_Click);
            btnOperatorGroup.btnSave.Click += new EventHandler(btnSave_Click);
            btnOperatorGroup.btnCancel.Click += new EventHandler(btnCancel_Click);
            btnOperatorGroup.btnQuit.Click += new EventHandler(btnQuit_Click);
        }

        //退出
        private void btnQuit_Click(object sender, EventArgs e)
        {
            if (dataState != Utility.CRUD.None)
            {
                if (MessageBox.Show("当前数据还未保存,是否需要保存数据?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    btnOperatorGroup.btnSave.Focus();
                    return;
                }
            }

            this.Close();
        }

        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.None;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);
            dgvVendor.Enabled = true;
            dgvVendor.Focus();
        }

        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            int recordId = 0;

            if (String.IsNullOrEmpty(txtVendorNo.Text.Trim()))
            {
                MessageBox.Show("供应商编号不能为空！", "Prompt ...");
                txtVendorNo.Focus();
                return;
            }

            if (String.IsNullOrEmpty(txtVendorName.Text.Trim()))
            {
                if (String.IsNullOrEmpty(txtVendorName.Text.Trim()))
                {
                    MessageBox.Show("供应商名称不能为空！", "Prompt ...");
                    txtVendorName.Focus();
                    return;
                }
            }

            VendorInfo vendorInfo = new VendorInfo();
            vendorInfo.VendorNo = txtVendorNo.Text.Trim();
            vendorInfo.VendorName = txtVendorName.Text.Trim();
            vendorInfo.ShortName = txtShortName.Text.Trim();
            vendorInfo.Contact = txtContact.Text.Trim();
            vendorInfo.ContactTitle = txtContactTitle.Text.Trim();
            vendorInfo.Tel = txtTel.Text.Trim();
            vendorInfo.Mobile = txtMobile.Text.Trim();
            vendorInfo.Fax = txtFax.Text.Trim();
            vendorInfo.Address = txtAddress.Text.Trim();
            vendorInfo.ZipCode = txtZipCode.Text.Trim();
            vendorInfo.BankAccount = txtBankAccount.Text.Trim();
            vendorInfo.Bank = txtBank.Text.Trim();
            vendorInfo.Credit = String.IsNullOrEmpty(txtCredit.Text.Trim()) ? 0 : Convert.ToDecimal(txtCredit.Text);
            vendorInfo.CreditPeriod = String.IsNullOrEmpty(txtCreditPeriod.Text.Trim()) ? 0 : Convert.ToInt32(txtCreditPeriod.Text);
            vendorInfo.TaxAccount = txtTaxAccount.Text.Trim();
            vendorInfo.Region = cmbRegion.SelectedValue.ToString();
            vendorInfo.VendorType = Convert.ToInt32(cmbVendorType.SelectedValue.ToString());
            vendorInfo.Remark = txtRemark.Text.Trim();
            vendorInfo.RecordId = dataState == Utility.CRUD.Insert ? 0 : (int)dgvVendor.CurrentRow.Cells["RecordId"].Value;

            vendor.WriteToDatabase(Utility.CRUDAction(dataState), vendorInfo, out recordId);

            if (dataState == Utility.CRUD.Insert)
            {
                /*
                dgvVendor.Rows.Add();
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["VendorNo"].Value = vendorInfo.VendorNo;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["VendorName"].Value = vendorInfo.VendorName;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["ShortName"].Value = vendorInfo.ShortName;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["Contact"].Value = vendorInfo.Contact;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["ContactTitle"].Value = vendorInfo.ContactTitle;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["Tel"].Value = vendorInfo.Tel;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["Mobile"].Value = vendorInfo.Mobile;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["Fax"].Value = vendorInfo.Fax;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["Address"].Value = vendorInfo.Address;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["ZipCode"].Value = vendorInfo.ZipCode;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["BankAccount"].Value = vendorInfo.BankAccount;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["Bank"].Value = vendorInfo.Bank;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["Credit"].Value = vendorInfo.Credit;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["CreditPeriod"].Value = vendorInfo.CreditPeriod;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["TaxAccount"].Value = vendorInfo.TaxAccount;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["Region"].Value = vendorInfo.Region;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["VendorType"].Value = vendorInfo.VendorType;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["TypeName"].Value = cmbVendorType.Text.Trim();
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["Remark"].Value = vendorInfo.Remark;
                dgvVendor.Rows[dgvVendor.NewRowIndex].Cells["RecordId"].Value = vendorInfo.RecordId;
                */

                DataRow dr = vendor.GetVendorByRecordId(recordId);
                dtVendor.ImportRow(dr);
                dgvVendor.CurrentRow.Selected = false;
                dgvVendor.Rows[dgvVendor.RowCount - 1].Selected = true;
                dgvVendor.CurrentCell = dgvVendor.Rows[dgvVendor.RowCount - 1].Cells["VendorNo"];     //让当前选中行的Cell得到焦点
                dgvVendor_SelectionChanged(sender, e);
            }
            else if (dataState == Utility.CRUD.Update)
            {
                dgvVendor.CurrentRow.Cells["VendorNo"].Value = vendorInfo.VendorNo;
                dgvVendor.CurrentRow.Cells["VendorName"].Value = vendorInfo.VendorName;
                dgvVendor.CurrentRow.Cells["ShortName"].Value = vendorInfo.ShortName;
                dgvVendor.CurrentRow.Cells["Contact"].Value = vendorInfo.Contact;
                dgvVendor.CurrentRow.Cells["ContactTitle"].Value = vendorInfo.ContactTitle;
                dgvVendor.CurrentRow.Cells["Tel"].Value = vendorInfo.Tel;
                dgvVendor.CurrentRow.Cells["Mobile"].Value = vendorInfo.Mobile;
                dgvVendor.CurrentRow.Cells["Fax"].Value = vendorInfo.Fax;
                dgvVendor.CurrentRow.Cells["Address"].Value = vendorInfo.Address;
                dgvVendor.CurrentRow.Cells["ZipCode"].Value = vendorInfo.ZipCode;
                dgvVendor.CurrentRow.Cells["BankAccount"].Value = vendorInfo.BankAccount;
                dgvVendor.CurrentRow.Cells["Bank"].Value = vendorInfo.Bank;
                dgvVendor.CurrentRow.Cells["Credit"].Value = vendorInfo.Credit;
                dgvVendor.CurrentRow.Cells["CreditPeriod"].Value = vendorInfo.CreditPeriod;
                dgvVendor.CurrentRow.Cells["TaxAccount"].Value = vendorInfo.TaxAccount;
                dgvVendor.CurrentRow.Cells["Region"].Value = vendorInfo.Region;
                dgvVendor.CurrentRow.Cells["VendorType"].Value = vendorInfo.VendorType;
                dgvVendor.CurrentRow.Cells["TypeName"].Value = cmbVendorType.Text.Trim();
                dgvVendor.CurrentRow.Cells["Remark"].Value = vendorInfo.Remark;
                dgvVendor.CurrentRow.Cells["RecordId"].Value = vendorInfo.RecordId;
            }
            else if (dataState == Utility.CRUD.Delete)
            {
                dgvVendor.Rows.Remove(dgvVendor.Rows[dgvVendor.CurrentRow.Index]);
            }

            dataState = Utility.CRUD.None;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);
            dgvVendor.Enabled = true;
            dgvVendor.Focus();
        }

        //删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvVendor.RowCount != 0)
            {
                if (MessageBox.Show("是否真的删除当前数据?", "删除数据", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    dataState = Utility.CRUD.Delete;
                    common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
                    dgvVendor.Enabled = false;
                }
            }
        }

        //编辑
        private void btnModify_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.Update;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            VendorInfo vendorInfo = new VendorInfo();
            vendorInfo.RecordId = (int)dgvVendor.CurrentRow.Cells["RecordId"].Value;

            common.SetControlsState(this, true);
            dgvVendor.Enabled = false;
            txtVendorNo.Enabled = false;
            txtVendorName.Focus();
        }

        //新增
        private void btnNew_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.Insert;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, true);
            dgvVendor.Enabled = false;

            txtVendorNo.Text = "";
            txtVendorName.Text = "";
            txtShortName.Text = "";
            txtContact.Text = "";
            txtContactTitle.Text = "";
            txtTel.Text = "";
            txtMobile.Text = "";
            txtFax.Text = "";
            txtAddress.Text = "";
            txtZipCode.Text = "";
            txtBankAccount.Text = "";
            txtBank.Text = "";
            txtCredit.Text = "";
            txtCreditPeriod.Text = "";
            txtTaxAccount.Text = "";
            cmbRegion.SelectedIndex = 0;
            cmbVendorType.SelectedIndex = 0;
            txtRemark.Text = "";

            txtVendorNo.Focus();
        }

        private void FrmVendor_Load(object sender, EventArgs e)
        {
            dtVendor = vendor.GetVendorList();

            dgvVendor.DataSource = dtVendor;
            dgvVendor.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            SetVendorDataGridView();

            cmbRegion.Tag = "R000";
            cmbVendorType.Tag = "V000";

            common.ComboBoxBindingDataSource(cmbRegion, "SubjectName", "SubjectName");
            common.ComboBoxBindingDataSource(cmbVendorType, "SubjectName", "SubjectId");

            if (dgvVendor.CurrentRow != null)
            {
                dgvVendor.CurrentRow.Selected = true;
            }

            dgvVendor.Focus();
        }

        private void SetVendorDataGridView()
        {
            dgvVendor.AllowUserToAddRows = false;
            dgvVendor.AllowUserToDeleteRows = false;
            dgvVendor.ReadOnly = true;

            dgvVendor.Columns[0].Width = 100;
            dgvVendor.Columns[0].Name = dtVendor.Columns["VendorNo"].ColumnName.ToString();
            dgvVendor.Columns[0].HeaderText = "供应商编号";
            dgvVendor.Columns[1].Width = 200;
            dgvVendor.Columns[1].Name = dtVendor.Columns["VendorName"].ColumnName.ToString();
            dgvVendor.Columns[1].HeaderText = "供应商名称";
            dgvVendor.Columns[2].Width = 100;
            dgvVendor.Columns[2].Name = dtVendor.Columns["ShortName"].ColumnName.ToString();
            dgvVendor.Columns[2].HeaderText = "简称";
            dgvVendor.Columns[3].Width = 80;
            dgvVendor.Columns[3].Name = dtVendor.Columns["Contact"].ColumnName.ToString();
            dgvVendor.Columns[3].HeaderText = "联系人";
            dgvVendor.Columns[4].Width = 80;
            dgvVendor.Columns[4].Name = dtVendor.Columns["ContactTitle"].ColumnName.ToString();
            dgvVendor.Columns[4].HeaderText = "称谓";
            dgvVendor.Columns[4].Frozen = true;
            dgvVendor.Columns[5].Width = 80;
            dgvVendor.Columns[5].Name = dtVendor.Columns["Tel"].ColumnName.ToString();
            dgvVendor.Columns[5].HeaderText = "电话";
            dgvVendor.Columns[6].Width = 80;
            dgvVendor.Columns[6].Name = dtVendor.Columns["Mobile"].ColumnName.ToString();
            dgvVendor.Columns[6].HeaderText = "移动电话";
            dgvVendor.Columns[7].Width = 80;
            dgvVendor.Columns[7].Name = dtVendor.Columns["Fax"].ColumnName.ToString();
            dgvVendor.Columns[7].HeaderText = "传真";
            dgvVendor.Columns[8].Width = 150;
            dgvVendor.Columns[8].Name = dtVendor.Columns["Address"].ColumnName.ToString();
            dgvVendor.Columns[8].HeaderText = "地址";
            dgvVendor.Columns[9].Width = 80;
            dgvVendor.Columns[9].Name = dtVendor.Columns["ZipCode"].ColumnName.ToString();
            dgvVendor.Columns[9].HeaderText = "邮编";
            dgvVendor.Columns[10].Width = 150;
            dgvVendor.Columns[10].Name = dtVendor.Columns["BankAccount"].ColumnName.ToString();
            dgvVendor.Columns[10].HeaderText = "银行账号";
            dgvVendor.Columns[10].Visible = false;
            dgvVendor.Columns[11].Width = 100;
            dgvVendor.Columns[11].Name = dtVendor.Columns["Bank"].ColumnName.ToString();
            dgvVendor.Columns[11].HeaderText = "开户行";
            dgvVendor.Columns[11].Visible = false;
            dgvVendor.Columns[12].Width = 80;
            dgvVendor.Columns[12].Name = dtVendor.Columns["Credit"].ColumnName.ToString();
            dgvVendor.Columns[12].HeaderText = "受信额度";
            dgvVendor.Columns[12].Visible = false;
            dgvVendor.Columns[13].Width = 80;
            dgvVendor.Columns[13].Name = dtVendor.Columns["CreditPeriod"].ColumnName.ToString();
            dgvVendor.Columns[13].HeaderText = "收款期限";
            dgvVendor.Columns[13].Visible = false;
            dgvVendor.Columns[14].Width = 150;
            dgvVendor.Columns[14].Name = dtVendor.Columns["TaxAccount"].ColumnName.ToString();
            dgvVendor.Columns[14].HeaderText = "税号";
            dgvVendor.Columns[14].Visible = false;
            dgvVendor.Columns[15].Width = 80;
            dgvVendor.Columns[15].Name = dtVendor.Columns["Region"].ColumnName.ToString();
            dgvVendor.Columns[15].HeaderText = "地区";
            dgvVendor.Columns[15].Visible = false;
            dgvVendor.Columns[16].Width = 100;
            dgvVendor.Columns[16].Name = dtVendor.Columns["VendorType"].ColumnName.ToString();
            dgvVendor.Columns[16].HeaderText = "类型";
            dgvVendor.Columns[16].Visible = false;
            dgvVendor.Columns[17].Width = 100;
            dgvVendor.Columns[17].Name = dtVendor.Columns["TypeName"].ColumnName.ToString();
            dgvVendor.Columns[17].HeaderText = "厂家类型";
            dgvVendor.Columns[17].Visible = false;
            dgvVendor.Columns[18].Width = 200;
            dgvVendor.Columns[18].Name = dtVendor.Columns["Remark"].ColumnName.ToString();
            dgvVendor.Columns[18].HeaderText = "备注";
            dgvVendor.Columns[18].Visible = false;
            dgvVendor.Columns[19].Width = 80;
            dgvVendor.Columns[19].Name = dtVendor.Columns["RecordId"].ColumnName.ToString();
            dgvVendor.Columns[19].HeaderText = "记录ID";
            dgvVendor.Columns[19].Visible = false;

            dgvVendor.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }

        private void txtVendorNo_Validating(object sender, CancelEventArgs e)
        {
            if (String.IsNullOrEmpty(txtVendorNo.Text.Trim()) == false)
            {
                if (vendor.IsUniqueVendorNo(txtVendorNo.Text.Trim(), dataState == Utility.CRUD.Insert ? 0 : (int)dgvVendor.CurrentRow.Cells["RecordId"].Value) == false)
                {
                    e.Cancel = true;
                    MessageBox.Show("当前供应商编号已存在！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtVendorNo.Select(0, txtVendorNo.Text.Trim().Length);
                }
            }
        }

        private void txtVendorName_Validating(object sender, CancelEventArgs e)
        {
            if (String.IsNullOrEmpty(txtVendorName.Text.Trim()) == false)
            {
                if (vendor.IsUniqueVendorNo(txtVendorName.Text.Trim(), dataState == Utility.CRUD.Insert ? 0 : (int)dgvVendor.CurrentRow.Cells["RecordId"].Value) == false)
                {
                    e.Cancel = true;
                    MessageBox.Show("当前供应商名称已存在！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtVendorName.Select(0, txtVendorName.Text.Trim().Length);
                }
            }
        }

        private void txtShortName_Validating(object sender, CancelEventArgs e)
        {
            if (String.IsNullOrEmpty(txtShortName.Text.Trim()) == false)
            {
                if (vendor.IsUniqueVendorNo(txtShortName.Text.Trim(), dataState == Utility.CRUD.Insert ? 0 : (int)dgvVendor.CurrentRow.Cells["RecordId"].Value) == false)
                {
                    e.Cancel = true;
                    MessageBox.Show("当前供应商简称已存在！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtShortName.Select(0, txtShortName.Text.Trim().Length);
                }
            }
        }

        private void dgvVendor_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvVendor.RowCount != 0)
            {
                txtVendorNo.Text = dgvVendor.CurrentRow.Cells["VendorNo"].Value.ToString();
                txtVendorName.Text = dgvVendor.CurrentRow.Cells["VendorName"].Value.ToString();
                txtShortName.Text = dgvVendor.CurrentRow.Cells["ShortName"].Value.ToString();
                txtContact.Text = dgvVendor.CurrentRow.Cells["Contact"].Value.ToString();
                txtContactTitle.Text = dgvVendor.CurrentRow.Cells["ContactTitle"].Value.ToString();
                txtTel.Text = dgvVendor.CurrentRow.Cells["Tel"].Value.ToString();
                txtMobile.Text = dgvVendor.CurrentRow.Cells["Mobile"].Value.ToString();
                txtFax.Text = dgvVendor.CurrentRow.Cells["Fax"].Value.ToString();
                txtAddress.Text = dgvVendor.CurrentRow.Cells["Address"].Value.ToString();
                txtZipCode.Text = dgvVendor.CurrentRow.Cells["ZipCode"].Value.ToString();
                txtBankAccount.Text = dgvVendor.CurrentRow.Cells["BankAccount"].Value.ToString();
                txtBank.Text = dgvVendor.CurrentRow.Cells["Bank"].Value.ToString();
                txtCredit.Text = dgvVendor.CurrentRow.Cells["Credit"].Value.ToString();
                txtCreditPeriod.Text = dgvVendor.CurrentRow.Cells["CreditPeriod"].Value.ToString();
                txtTaxAccount.Text = dgvVendor.CurrentRow.Cells["TaxAccount"].Value.ToString();
                cmbRegion.SelectedValue = dgvVendor.CurrentRow.Cells["Region"].Value.ToString();
                cmbVendorType.SelectedValue = dgvVendor.CurrentRow.Cells["VendorType"].Value.ToString();
                txtRemark.Text = dgvVendor.CurrentRow.Cells["Remark"].Value.ToString();
            }
        }

    }
}
